package ac.java.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;

public class AcFile {
	public static String readFile(String path) throws IOException {
		return readFile(new File(path));
	}

	public static String readFile(File fs) throws IOException {
		StringBuffer buffer = new StringBuffer();
		FileInputStream is = new FileInputStream(fs);
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		String line = reader.readLine(); // 读取第一行
		while (line != null) { // 如果 line 为空说明读完了
			buffer.append(line); // 将读到的内容添加到 buffer 中
			buffer.append("\n"); // 添加换行符
			line = reader.readLine(); // 读取下一行
		}

		return buffer.toString();
	}

	static public boolean saveFile(String path, String cot) {
		try {
			FileWriter fw = new FileWriter(path);
			fw.write(cot, 0, cot.length());
			fw.flush();
			fw.close();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public static long copy(String srcPath, String destPath) {
		File srcFile = new File(srcPath);
		File destFile = new File(destPath);
		return copy(srcFile, destFile);
	}

	public static long copy(File srcFile, File destFile) {
		long copySizes = 0;
		if (!srcFile.exists()) {
			return -1;
		} else if (destFile.exists()) {
			return -2;
		}

		try {
			FileChannel fcin = new FileInputStream(srcFile).getChannel();
			FileChannel fcout = new FileOutputStream(destFile).getChannel();

			ByteBuffer buff = ByteBuffer.allocate(1024);
			int b = 0, i = 0;

			long size = fcin.size();
			fcin.transferTo(0, fcin.size(), fcout);
			fcin.close();
			fcout.close();
			copySizes = size;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return copySizes;
	}

	public static boolean reName(String srcPath, String destPath) {
		File srcFile = new File(srcPath);
		File destFile = new File(destPath);
		return reName(srcFile, destFile);
	}

	public static boolean reName(File srcFile, File destFile) {
		long rs = copy(srcFile, destFile);
		if (rs > 0) {
			srcFile.delete();
			return true;
		}

		return false;
	}

	public static Map listFiles(String path) {
		File fs = new File(path);
		return listFiles(fs);
	}

	public static Map listMd5Files(String path) {
		return listMd5Files(new File(path));
	}

	public static Map listMd5Files(File fs) {
		if (!fs.exists() || !fs.isDirectory()) {
			return null;
		}

		Map dMap = new HashMap<String, File>();
		Map fMap = new HashMap<String, File>();

		File[] fsList = fs.listFiles();

		try {
			for (int i = 0; i < fsList.length; i++) {
				if (fsList[i].isDirectory()) {
					dMap.put(fsList[i].getAbsolutePath(), "dir");

					Map<String, Map> nRs = listMd5Files(fsList[i]);
					if (null != nRs) {
						if (null != nRs) {
							if (nRs.containsKey("d")) {
								dMap.putAll(nRs.get("d"));
							}

							if (nRs.containsKey("f")) {
								fMap.putAll(nRs.get("f"));
							}
						}
					}
				} else {
					fMap.put(fsList[i].getAbsolutePath(), AcFileMd5
							.getCheckCode(fsList[i]));
				}
			}
		} catch (Exception e) {
			return null;
		}

		Map<String, Map> rsMap = new HashMap<String, Map>();
		rsMap.put("d", dMap);
		rsMap.put("f", fMap);

		return rsMap;
	}

	public static Map<String, Map> listFiles(File fs) {
		if (!fs.exists() || !fs.isDirectory()) {
			return null;
		}

		Map dMap = new HashMap<String, File>();
		Map fMap = new HashMap<String, File>();

		File[] fsList = fs.listFiles();
		for (int i = 0; i < fsList.length; i++) {
			if (fsList[i].isDirectory()) {
				dMap.put(fsList[i].getAbsolutePath(), "dir");
				Map<String, Map> nRs = listFiles(fsList[i]);
				if (null != nRs) {
					if (nRs.containsKey("d")) {
						dMap.putAll(nRs.get("d"));
					}

					if (nRs.containsKey("f")) {
						fMap.putAll(nRs.get("f"));
					}
				}
			} else {
				fMap.put(fsList[i].getAbsolutePath(), fsList[i]);
			}
		}

		Map<String, Map> rsMap = new HashMap<String, Map>();
		rsMap.put("d", dMap);
		rsMap.put("f", fMap);

		return rsMap;
	}

	public static boolean deleteDir(File fs) 
	{
		return deleteDir(fs.getAbsolutePath());
	}
	
	public static boolean deleteDir(String filepath)
	{
		File f = new File(filepath);// 定义文件路径
		if (f.exists() && f.isDirectory())
		{	
			// 判断是文件还是目录
			if (f.listFiles().length > 0) {// 若目录下没有文件则直接删除
				// 若有则把文件放进数组，并判断是否有下级目录
				File delFile[] = f.listFiles();
				int i = f.listFiles().length;
				for (int j = 0; j < i; j++) {
					if (delFile[j].isDirectory()) {
						// 递归调用del方法并取得子目录路径
						if(!deleteDir(delFile[j].getAbsolutePath()))
						{
							return false;
						}
					}
					else
					{
						 // 删除文件
						if(!delFile[j].delete())
						{
							return false;
						}
					}
				}
			}
			
			if (f.listFiles().length == 0) {// 若目录下没有文件则直接删除
				return f.delete();
			}
		}
		
		return true;
	}
	
	public static boolean deleteFile(String filepath)
	{
		File f = new File(filepath);// 定义文件路径
		if (f.exists())
		{
			return f.delete();
		}
		
		return true;
	}
}
